package com.fp.m7.dao;

import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.fp.m7.model.Account;
import com.fp.m7.model.Comment;
import com.fp.m7.repository.CommentRepository;

@Transactional
public class CommentDaoImpl extends BaseDaoImpl<Comment, CommentRepository>
		implements CommentDao {

	@Override
	public List<Comment> findUsersLatestComments(Account account,
			int maxPerPage, int pageNo) {
		if (maxPerPage < 0) {
			throw new IllegalArgumentException("Negative query limit=" + maxPerPage);
		}
		if (pageNo < 0) {
			throw new IllegalArgumentException("Negative query skip=" + pageNo);
		}
		return repository.findUsersLatestComments(account, maxPerPage, pageNo * maxPerPage);
	}

	@Override
	public List<Comment> findUsersCommentsByDate(Account account, Date from, Date to) {
		return repository.findUsersCommentsByDate(account, from.getTime(), to.getTime());
	}

	@Override
	public List<Comment> findCommentFeedForUser(Account account,
			int maxPerPage, int pageNo) {
		if (maxPerPage < 0) {
			throw new IllegalArgumentException("Negative query limit=" + maxPerPage);
		}
		if (pageNo < 0) {
			throw new IllegalArgumentException("Negative query skip=" + pageNo);
		}
		return repository.findCommentFeedForUser(account, maxPerPage, pageNo * maxPerPage);
	}

	@Override
	public int removeUsersComments(Account account) {
		List<Comment> comments = repository.findAllCommentsByUser(account);
		repository.delete(comments);
		return comments.size();
	}

}
